/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: HP
 * Date: 2023 -04 -07
 * Time: 17:33
 */
// 1、无头单向非循环链表实现
public class SingleLinkedList {
    class Node {
        public int val;
        public Node next;

        public Node(int val) {
            this.val = val;
        }
    }

    private Node head;

    //头插法
    public void addFirst(int data) {
        Node node = new Node(data);
        node.next = head;
        head = node;
    }

    //尾插法
    public void addLast(int data) {
        if(head==null){
            head=new Node(data);
            return;
        }
        Node cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        Node node = new Node(data);
        cur.next = node;
    }

    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index, int data) {
        int size = size();
        if (index < 0 || index > size) {
            throw new RuntimeException("add index out of the range!");
        }
        if (index == 0) {
            addFirst(data);
        } else if (index == size) {
            addLast(data);
        } else {
            int cnt = 1;
            Node cur = head;
            while (cnt != index) {
                cnt++;
                cur = cur.next;
            }
            Node node = new Node(data);
            node.next = cur.next;
            cur.next = node;
        }
        return true;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        Node cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if (head == null) return;
        if (head.val == key) {
            head = head.next;
            return;
        }
        Node pre = head;
        Node cur = head.next;
        while (cur != null) {
            if (cur.val == key) {
                pre.next = cur.next;
                return;
            } else {
                pre = cur;
                cur = cur.next;
            }
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if (head == null) return;
        Node pre = head;
        Node cur = head.next;
        while (cur != null) {
            if (cur.val == key) {
                pre.next = cur.next;
                cur = cur.next;
            } else {
                pre = cur;
                cur = cur.next;
            }
        }
        if (head.val == key) {
            head = head.next;
        }
    }

    //得到单链表的长度
    public int size() {
        int cnt = 0;
        Node cur = head;
        while (cur != null) {
            cnt++;
            cur = cur.next;
        }
        return cnt;
    }

    public void display() {
        Node cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void clear() {
        head = null;
    }

    public static void main(String[] args) {
        SingleLinkedList sll=new SingleLinkedList();
        sll.addLast(1);
        sll.addLast(2);
        sll.addLast(6);
        sll.addLast(2);
        sll.addLast(5);
        sll.addLast(2);
        sll.addLast(6);
        sll.addFirst(3);
        sll.addFirst(2);
        sll.addFirst(3);
        sll.addFirst(2);
        sll.addFirst(4);
        sll.addFirst(5);
        sll.display();
        sll.addIndex(0,21);
        sll.addIndex(sll.size(),2);
        sll.addIndex(5,11);
        sll.display();
        sll.remove(1);
        sll.removeAllKey(2);
        sll.remove(21);
        System.out.println(sll.contains(7));
        sll.display();
    }
}
